var app = app || {};
app.snow = {};
app.snow.num = 50;
app.snow.speedX = new Array(app.snow.num);
app.snow.speedY = new Array(app.snow.num);
app.snow.fps = 24;
app.snow.snow = new Array(app.snow.num);
app.snow.snowX = new Array(app.snow.num);
app.snow.snowY = new Array(app.snow.num);
app.snow.snowScale = new Array(app.snow.num);
app.snow.stageX = 320;
app.snow.stageY = 300;

(function($) {
	$(function() {
		var snow = app.snow;
		var $body = $('body');
		var div = '';
		
		for ( var i = 0; i < snow.num; i++) {
			snow.snowX[i] = random(0, snow.stageX);
			snow.snowY[i] = random(-snow.stageY, 0);
			snow.speedX[i] = 2 / (0.02 * i + 1);
			snow.speedY[i] = 5 / (0.02 * i + 1);
			snow.snowScale = 10 / (0.02 * i + 1);
			div += '<div id="snow' + i + '" style="width: ' + snow.snowScale
					+ 'px; height: ' + snow.snowScale + 'px; left: '
					+ snow.snowX[i] + 'px; top: ' + snow.snowY[i]
					+ 'px;-webkit-border-radius: ' + snow.snowScale
					+ 'px;	background-color: snow;'
					+ '-webkit-transition-duration: ' + (1 / snow.fps) + 's;'
					+ ' position: absolute;"></div>';
		}

		$body.append(div);

		for (i = 0; i < snow.num; i++) {
			var $snow = $('#snow' + i);
			snow.snow[i] = $snow;
		}

		falling();
	});

	function falling() {
		var snow = app.snow;
		var move = setInterval(function() {
			for ( var i = 0; i < snow.num; i++) {
				snow.snowX[i] += ((Math.random() * 2) - 1) * snow.speedX[i];
				
				if (snow.snowY[i] > snow.stageY) {
					snow.snowY[i] = -20;
					snow.snow[i].css('-webkit-transition-duration', '0s');
				} else if (snow.snowY[i] < 0) {
					snow.snowY[i] += (Math.random() + 1) * snow.speedY[i];
					snow.snow[i].css('-webkit-transition-duration',
							(1 / snow.fps) + 's');
				} else {
					snow.snowY[i] += (Math.random() + 1) * snow.speedY[i];
				}

				snow.snow[i].css('left', snow.snowX[i]);
				snow.snow[i].css('top', snow.snowY[i]);
			}
		}, (1000 / snow.fps));
	}

	function random(low, high) {
		return (Math.random() * (high - low)) + low;
	}
})(jQuery);